﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp10
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        //插表
        private void Form2_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";

            var res = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = res;

            dataGridView1.ReadOnly = true;//只读属性

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//选整行

            dataGridView1.AllowUserToAddRows = false;//去最后一行空白行
        }
        //模糊查询
        private void button1_Click(object sender, EventArgs e)
        {

            var Title = textBox1.Text;

            var sql = string.Format("select * from Articles where Title like '%{0}%'", Title);

            var res = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = res;
        }
        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            form3.Show();
        }
        
        private void button2_Click_1(object sender, EventArgs e)
        {
           
        }
        //修改
        private void button2_Click_2(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
               
                Form3 form3 = new Form3(id,title,author);
                var res = form3.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var Title = textBox1.Text;

                    var sql = string.Format("select * from Articles where Title like '%{0}%'", Title);

                    var ress = DbHelper.GetDataTable(sql);

                    dataGridView1.DataSource = ress;
                }
            }
            else
            {
                MessageBox.Show("无选中对象", "提示");
            }
            
        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count>0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确定删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if(res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where id={0}", id);

                    var ress = DbHelper.AddOrUpdateOrDelete(sql);

                    if(ress>0)
                    {
                        var Title = textBox1.Text;

                        var sqll = string.Format("select * from Articles where Title like '%{0}%'", Title);

                        var resss = DbHelper.GetDataTable(sqll);

                        dataGridView1.DataSource = resss;

                        MessageBox.Show("删除成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("没有选择对象", "提示");
                    }
                }
            }
        }
    }
}
